import jweixin from 'jweixin-module';
import Http from '@/utils/Http.js';
import Tools from '@/utils/Tools.js';
export default {
	isWechat() {
		return Tools.isWechat();
	},
	initJssdk(callback, jsApiList = [], url = '') {
		// #ifdef H5
		if (!url) {
			url = encodeURIComponent(location.href.split('#')[0]);
		}
		if (this.isWechat()) {
			Http.get(Http.api.getWxConfig, {
				url
			}, res => {
				setTimeout(() => {
					jweixin.config({
						debug: false,
						appId: res.data.signPackage.appId,
						timestamp: res.data.signPackage.timestamp,
						nonceStr: res.data.signPackage.nonceStr,
						signature: res.data.signPackage.signature,
						jsApiList: [
							'updateAppMessageShareData',
							'onMenuShareTimeline',
							'openAddress',
							'getLocation',
							'openLocation',
							'chooseWXPay',
						]

					});

					jweixin.ready(() => {
						callback && callback();
					});
					jweixin.error((e) => {
						console.log(e)
						const entryUrl = uni.getStorageSync('entryUrl') || '';
						if (url != entryUrl && entryUrl) {
							this.initJssdk(callback, jsApiList, entryUrl)
						}
					})

				}, 0)



			})
		}
		// #endif
	},
	...jweixin
}
